## 读取xml 获取车辆长宽数据 绘制图像
import  xml.dom.minidom as xd
import os
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
def get_size(xml_name):
    dom = xd.parse(xml_name)
    root=dom.documentElement
    objects=root.getElementsByTagName('object')
    size = []
    # size_num = {"0-100":[],"100-200":[],"200-300":[],"300-400":[],"400-512":[]}
    # size_num = [0,0,0,0,0]
    for ob in objects:
        # name = ob.getElementsByTagName('name')[0]
        bndbox = ob.getElementsByTagName('bndbox')[0]
        xmin = bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue
        ymin = bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue
        xmax = bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue
        ymax = bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue
        # print('xmin = {} ymin ={} xmax = {} ymax ={}'.format(xmin,ymin,xmax, ymax))
        img_h = int(ymax) - int(ymin)
        img_w = int(xmax) - int(xmin)
        
        size.append([img_w,img_h])
    return size
def DrawScatter(car_size,size_num):
        #    绘制两组数据的散点图
        plt.figure(figsize=(10, 10), dpi=100)
        ax = plt.subplot(111)
        # 导入中文字体，及字体大小
        zhfont = FontProperties(fname='C:/Windows/Fonts/simsun.ttc', size=20)

        p1 = ax.scatter([size[0] for size in car_size], [size[1] for size in car_size], marker='o', s=10, facecolors='none',edgecolors='k')
        p2 = ax.plot([x for x in range(512)],[x for x in range(512)],color = 'r',linewidth = 2,linestyle = '-.')
        p2 = ax.plot([x for x in range(512)],[x*0.5 for x in range(512)],color = 'b',linewidth = 2,linestyle = '-.')
        p2 = ax.plot([x*0.5 for x in range(512)],[x for x in range(512)],color = 'y',linewidth = 2,linestyle = '-.')
        for i,num in enumerate(size_num):
            plt.text(i*100, 0, 'N={}'.format(num),fontproperties=zhfont,fontsize=13, color = "b")
        ax.yaxis.grid(True, which='major')
        ax.xaxis.grid(True, which='major')
        plt.xlabel(u'Width', fontproperties=zhfont)
        plt.ylabel(u'Hight', fontproperties=zhfont)
        plt.title(u'苫布车辆尺寸', fontproperties=zhfont)


        # plt.xticks([w for w in range(1, len(Voladd) + 1)], ['%s' % w for w in time])
        #         # plt.xticks(range(0, len(Voladd1) + 1, 180))
        # plt.xticks(self.LoadingTimeLabelX[0], self.LoadingTimeLabelX[1])
        # ax.legend((p781, p225,p226,p225_6,p239,p240), (u'781', u'体积仪_225', u'体积仪_226', u'体积仪_225+6',u'体积仪_239',u'体积仪_240'),loc='upper left', prop=zhfont)
        # ax.legend((p781,p239,p240), (u'781', u'体积仪_239',u'体积仪_240'),loc='upper left', prop=zhfont)
        # 设定 坐标轴的取值范围
        plt.axis([0, 512, 0, 512])
        # 以当前时间命名图片
        imgname = "./car_size.png"
        plt.savefig(imgname, bbox_inches = 'tight')
        # plt.show()
def main():
    xml_path = r'E:\2019智工之眼项目工作\20200520苫布数据集\Annotations'
    xml_names = os.listdir(xml_path)
    all_car_size = []
    size_num = [0,0,0,0,0,0]
    for xml_name in xml_names:
        car_size = get_size(os.path.join(xml_path,xml_name)) 
        all_car_size.extend(car_size)  
        for size in car_size:
            # print(np.max([size[0],size[1]]))
            # max_size = np.max([size[0],size[1]])
            # print(max_size/100)
            size_num[int(np.max([size[0],size[1]])/100)] += 1
    # print(all_car_size) 
    DrawScatter(all_car_size,size_num)

if __name__ == '__main__':
    main()